//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

package uxgenes.ext.ads;


import uxgenes.core.Logger;
import android.app.Activity;



// #ifdef ADMOB
import com.google.ads.*;
import com.google.ads.AdRequest.ErrorCode;
// #endif ADMOB


//
// Ad Mob Interstitial
//
public final class AdMobInterstitial implements 	uxgenes.ext.IExtension
											// #ifdef ADMOB
											, AdListener
											// #endif ADMOB
{
// variables //
	
	private final static String	TAG = "AdMobUtils";

	// #ifdef ADMOB
	private final Activity	_activity;
	private final String	_adInterstitialId;
	private InterstitialAd 	_interstitial = null;
	private boolean			_showInterstitialAfterLoading = false;
	// #endif ADMOB

	
	
// methods //
	
	// constructor
	public AdMobInterstitial (Activity act, String interstitialId)
	{
		// #ifdef ADMOB
		this._activity = act;
		this._adInterstitialId = interstitialId;
		// #endif ADMOB
	}

	
	
// Events //

	// OnAdded
	public final void OnAdded (uxgenes.core.EngineContext eng) {}
	
	
	// OnRemoved
	public final void OnRemoved () {}
	
	
	// OnInit
	public final void OnInit () {}
	
	
	// OnDestroy
	public final void OnDestroy ()
	{
		if ( _interstitial != null ) {
			_interstitial = null;
		}
	}
	
	
	// OnResume
	public final void OnResume () {}

	
	// OnStart
	public final void OnStart () {}
	
	// OnPause
	public final void OnPause () {}

	// OnStop
	public final void OnStop () {}

	

// Interstitial //
	
	// InitInterstitial
	public final void InitInterstitial ()
	{
		// #ifdef ADMOB
	    _interstitial = new InterstitialAd( _activity, _adInterstitialId );
	    _interstitial.loadAd( new AdRequest() );
	    _interstitial.setAdListener( this );
		/* #else ADMOB
		Logger.W( TAG, "AdMob interstitial not supported" );
		// #endif ADMOB */
	}

	
	// ShowInterstitial
	public final void ShowInterstitial (boolean waitLoading)
	{
		// #ifdef ADMOB
		if ( _interstitial != null )
		{
			if ( _interstitial.isReady() ) {
				_interstitial.show();
				_showInterstitialAfterLoading = false;
			}
			else {
				_showInterstitialAfterLoading = waitLoading;
			}
		}
		/* #else ADMOB
		Logger.W( TAG, "AdMob interstitial not supported" );
		// #endif ADMOB */
	}


	// LoadInterstitial
	public final void LoadInterstitial () {
		if ( _interstitial != null ) {
			_interstitial.loadAd( new AdRequest() );
		}
	}

	
	// IsInterstitialInitialized
	public final boolean IsInterstitialInitialized ()
	{
		// #ifdef ADMOB
		return _interstitial != null;
		/* #else ADMOB
		return false;
		// #endif ADMOB */
	}

	
	// IsInterstitialLoaded
	public final boolean IsInterstitialLoaded ()
	{
		// #ifdef ADMOB
		return _interstitial.isReady();
		/* #else ADMOB
		return false;
		// #endif ADMOB */
	}


	
// AdListener //

// #ifdef ADMOB
	
	// onDismissScreen
	public final void onDismissScreen (Ad ad) {
	}


	// onFailedToReceiveAd
	public final void onFailedToReceiveAd (Ad ad, ErrorCode err) {
	}


	// onLeaveApplication
	public final void onLeaveApplication (Ad ad) {
	}


	// onPresentScreen
	public final void onPresentScreen (Ad ad) {
		Logger.W( "AdMob interstitial shown" );
	}


	// onReceiveAd
	public final void onReceiveAd (Ad ad) {
		if ( ad == _interstitial && _showInterstitialAfterLoading ) {
			_interstitial.show();
			_showInterstitialAfterLoading = false;
		}
	}

//#endif ADMOB
	
}
